﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Collections;
using Aspose.Words;
using Maticsoft.DBUtility;

namespace SkWeb.FASJ
{
    public partial class SchemeDesign : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            /*
            string sqlStr = "select name,text,field_name,default_varl, multi_val,formula from tb_bookmark1";
            DataTable dt = DbHelperOra.Query(sqlStr).Tables[0];
            string strId = "56228";
            sqlStr = "select * from skgysjb where id ="+strId;
            DataTable dt_data = DbHelperOra.Query(sqlStr).Tables[0];
            if (dt_data.Rows.Count < 1)
            {
                Response.Write("查询数据不存在");
                Response.End();
                return;
            }
            DataRow dr_data = dt_data.Rows[0];

            List<TableCell> cells = new List<TableCell>();

            foreach (DataRow dr in dt.Rows)
            {
                TableRow tr = new TableRow();
                TableCell cell_lab = new TableCell();
                cell_lab.Text =Convert.ToString(dr["text"]);
                TableCell cell_input = new TableCell();
                
                if (Convert.IsDBNull(dr["multi_val"]))
                {
                    TextBox tb = new TextBox();
                    if (!Convert.IsDBNull(dr["field_name"]))
                    {
                        if (!Convert.IsDBNull(dr_data[Convert.ToString(dr["field_name"])]))
                            tb.Text = Convert.ToString(dr_data[Convert.ToString(dr["field_name"])]);
                        else
                            tb.Text = Convert.ToString(dr["default_varl"]); 
                    }
                    else
                        tb.Text = Convert.ToString(dr["default_varl"]);

                    if (!Convert.IsDBNull(dr["text"]))
                        tb.ID = Convert.ToString(dr["text"]);
                    if (!Convert.IsDBNull(dr["formula"]))
                    {
                        string strFormula = Convert.ToString( dr["formula"]);
                        string[] strFormulas = strFormula.Split('=');
                        strFormula = "";
                        for (int i = 0; i < strFormulas.Length; i++)
                        {
                            if (i % 2 == 0)
                            {
                                 strFormulas[i]=strFormulas[i].Replace("(", "document.getElementById(\'");
                                 strFormulas[i]=strFormulas[i].Replace(")", "\').value");
                                 strFormula += strFormulas[i]+"=";
                            }
                            else
                            {
                                 strFormulas[i]=strFormulas[i].Replace("(", "Number(document.getElementById(\'");
                                 strFormulas[i]=strFormulas[i].Replace(")", "\').value)");
                                 strFormula += strFormulas[i];
                            }
                            
                        }
                        
                        tb.Attributes.Add("onblur", strFormula);
                    }
                    cell_input.Controls.Add(tb);
                   
                }
                else
                {
                    string strItems = Convert.ToString(dr["multi_val"]);
                    string [] items =  strItems.Split(';');
                    DropDownList ddl = new DropDownList();
                    foreach(string item in items)
                    {
                        ddl.Items.Add(item);
                    }
                    if (!Convert.IsDBNull(dr["field_name"]))
                        ddl.SelectedValue = Convert.ToString(dr_data[Convert.ToString(dr["field_name"])]);
                    if (!Convert.IsDBNull(dr["text"]))
                        ddl.ID = Convert.ToString(dr["text"]);
                    cell_input.Controls.Add(ddl);
                }
                tr.Cells.Add(cell_lab);
                tr.Cells.Add(cell_input);
                this.tab_data.Rows.Add(tr);
            }*/
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            string sqlStr = "select name,text,field_name,default_varl, multi_val,formula ,xs from tb_bookmark1 order by xh";
            DataTable dt = DbHelperOra.Query(sqlStr).Tables[0];
            string strId = this.HiddenField1.Value.Trim();//"56228";
            //sqlStr = "select * from skgysjb where id =" + strId ;
            sqlStr = "select t.*,a.qcyzsd from skgysjb t   left join zysgsj a on a.id = t.id where  t.id= " + strId;
            bool qcyzIsNull = true;
            DataTable dt_data = DbHelperOra.Query(sqlStr).Tables[0];
            if (dt_data.Rows.Count < 1)
            {
                Response.Write("查询数据不存在");
                Response.End();
                return;
            }
            DataRow dr_data = dt_data.Rows[0];

            DropDownList ddl_qxcs = new DropDownList();
            ddl_qxcs.ID = "枪型参数";
            ddl_qxcs.CssClass = "select";
            ddl_qxcs.Items.Add("73");
            ddl_qxcs.Items.Add("89");
            ddl_qxcs.Items.Add("102");


            List<TableCell> cells = new List<TableCell>();
            this.tab_data.Rows.Clear();
            TableCell tc_ = new TableCell();
            TableRow tr_ = new TableRow();
            tc_.ColumnSpan = 2;
            tc_.Text = "基础数据";
            tc_.CssClass = "tab_header";
            tr_.Cells.Add(tc_);
            tab_data.Rows.Add(tr_);
            foreach (DataRow dr in dt.Rows)
            {
                TableRow tr = new TableRow();
                TableCell cell_lab = new TableCell();
                cell_lab.Width=120;
               // cell_lab.Text = Convert.ToString(dr["text"]);
                cell_lab.Text = Convert.ToString(dr["name"]);
                TableCell cell_input = new TableCell();
                cell_input.Width = 150;
                if (Convert.IsDBNull(dr["multi_val"]))
                {
                    TextBox tb = new TextBox();
                    if (!Convert.IsDBNull(dr["field_name"]))
                    {
                        string fieldVal = Convert.ToString(dr["field_name"]);
                        if (fieldVal == "_TIME_NOW_")
                        {
                            tb.Text = DateTime.Now.ToString("yyyy年MM月dd日");

                        }
                        else
                        {

                            if (!Convert.IsDBNull(dr_data[Convert.ToString(dr["field_name"])]))
                                tb.Text = Convert.ToString(dr_data[Convert.ToString(dr["field_name"])]) == "0" ? "—" : Convert.ToString(dr_data[Convert.ToString(dr["field_name"])]);
                            else
                                tb.Text = Convert.ToString(dr["default_varl"]) == "0" ? "—" : Convert.ToString(dr["default_varl"]);
                            if (cell_lab.Text == "前磁遇阻深度")
                            {

                                if (tb.Text != "—")
                                {
                                    qcyzIsNull = false;
                                }
                            }
                            if (cell_lab.Text =="阻流环深度")
                            {
                                if (!qcyzIsNull)
                                {
                                    tb.Text = "—";
                                }
                            }

                        }
                    }
                    else
                        tb.Text = Convert.ToString(dr["default_varl"]);

                    string strFunction = "";
                    if (!Convert.IsDBNull(dr["text"]))
                        tb.ID = Convert.ToString(dr["text"]);
                    if (!Convert.IsDBNull(dr["formula"]))
                    {
                        string strFormula = Convert.ToString(dr["formula"]);
                        strFormula = strFormula.Replace("；",";");
                        
                        string[] arrFromula = strFormula.Split(';');
                        for (int n = 0; n < arrFromula.Length; n++)
                        {
                            strFormula = arrFromula[n];
                            if (String.IsNullOrEmpty(strFormula))
                            {
                                continue; 
                            }

                            string[] strFormulas = strFormula.Split('=');
                            strFormula = "";
                            string obj = "";
                            for (int i = 0; i < strFormulas.Length; i++)
                            {
                                if (i % 2 == 0)
                                {
                                    strFormulas[i] = strFormulas[i].Replace("(", "document.getElementById(\'");
                                    strFormulas[i] = strFormulas[i].Replace(")", "\').value");
                                    strFormula += strFormulas[i] + "=";
                                    obj = strFormulas[i];
                                }
                                else
                                {
                                    strFormulas[i] = strFormulas[i].Replace("(", "Number(document.getElementById(\'");
                                    strFormulas[i] = strFormulas[i].Replace(")", "\').value)");
                                    strFormula += strFormulas[i] + ";";
                                    if (!String.IsNullOrEmpty(obj))
                                        strFormula += obj + "=Number(" + obj + ").toFixed(2)";
                                    obj = "";
                                }

                            }
                            strFunction += strFormula + ";";
                        }
                        if (cell_lab.Text == "阻流环深度")
                        {
                            strFunction += "alert('将文档中的前磁遇阻深度改为阻流环深度');";
                        }
                        tb.Attributes.Add("onblur", strFunction);
                    }
                    if (cell_lab.Text == "阻流环深度")
                    {
                        if (strFunction == "")
                        {
                            strFunction += "alert('将文档中的前磁遇阻深度改为阻流环深度');";
                            tb.Attributes.Add("onblur", strFunction);
                        }
                    }
                    
                    cell_input.Controls.Add(tb);

                }
                else
                {
                    string strItems = Convert.ToString(dr["multi_val"]);
                    string[] items = strItems.Split(';');
                    DropDownList ddl = new DropDownList();
                    ddl.CssClass = "select";
                    foreach (string item in items)
                    {
                        ddl.Items.Add(item);
                    }
                    if (!Convert.IsDBNull(dr["field_name"]))
                    {
                        if (Convert.ToString(dr["text"]) == "枪型")
                        {
                            string qx = Convert.ToString(dr_data[Convert.ToString(dr["field_name"])]);
                            
                            ddl.SelectedValue = qx.Split('-')[0];
                            //this.Button1.Text = qx + " " + qx.Split('-')[0];
                          //  Response.Write(qx);
                           // Response.Write(qx.Split('-')[0]);
                            ddl_qxcs.SelectedValue = qx.Split('-')[1];
                        }
                        else
                        {
                            ddl.SelectedValue = Convert.ToString(dr_data[Convert.ToString(dr["field_name"])]);
                        }
                    }
                    if (!Convert.IsDBNull(dr["text"]))
                        ddl.ID = Convert.ToString(dr["text"]);
                   

                    if (Convert.ToString(dr["text"]) == "井别")
                    {
                        ddl.Attributes.Add("onchange", "showInput(this)");
                    }
                    cell_input.Controls.Add(ddl);

                }
                tr.Cells.Add(cell_lab);
                if (cell_lab.Text == "枪型")
                {
                    cell_input.Controls.Add(ddl_qxcs);
                }
                tr.Cells.Add(cell_input);
                if (Convert.IsDBNull(dr["xs"]) || Convert.ToInt32(dr["xs"]) == 0)
                    tr.Attributes.Add("style", "display:none");

                this.tab_data.Rows.Add(tr);
                
            }
            TableRow tr1 = new TableRow();
            TableCell tc = new TableCell();
            //tc.ColumnSpan = 2;
            //tc.Text = " <a id='aSubmitRedirect' href='javascript:SubmitRedirect()' class='easyui-linkbutton'>提交并选择方案</a>";
            //tr1.Cells.Add(tc);
            tab_data.Rows.Add(tr1);


        }
    }
}